package com.fancon.android.emoji.widget;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow.OnDismissListener;

import com.fancon.android.emoji.Emoji;
import com.fancon.android.emoji.EmojiKeyBoard;
import com.fancon.android.emoji.EmojiUtil;
import com.fancon.android.emoji.IRichGlobalState;
import com.fancon.android.emoji.EmojiKeyBoard.EmojiKeyBoardListener;

public class M05RichText extends EditText{
	protected Context mContext;
	protected EmojiUtil mEmojiUtil;
	protected View emojiView;
	public M05RichText(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		mEmojiUtil = ((IRichGlobalState) (((Activity)mContext).getApplication())).getEmojiUtil();
		((Activity) mContext).registerForContextMenu(this);
	}

	public M05RichText(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		mEmojiUtil = ((IRichGlobalState) (((Activity)mContext).getApplication())).getEmojiUtil();
		((Activity) mContext).registerForContextMenu(this);
	}

	public M05RichText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mContext = context;
		mEmojiUtil = ((IRichGlobalState) (((Activity)mContext).getApplication())).getEmojiUtil();
		((Activity) mContext).registerForContextMenu(this);
	}
	public void setText(String utf8String){
		Spanned displaySpanned = mEmojiUtil.toEmojiText(utf8String);
		super.setText(displaySpanned);
	}
	public Editable getText(){
		return super.getText();
	}
	public String getRichText(){
		String utf8 = mEmojiUtil.toUnicode(super.getText());
		//Fix \n\n for rich edit text
		return RichEditText.removeFeedLine(utf8);
	}
	
	public void hideKeyBoard(){
		InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(getWindowToken(), 0);
	}
	public void showKeyBoard(){
		//InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		// only will trigger it if no physical keyboard is open
		//mgr.showSoftInput(RichEditText.this, InputMethodManager.SHOW_IMPLICIT);
		//Log.d("show keyboard", "show keyboard");
		InputMethodManager inputMgr = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMgr.toggleSoftInput(0, 0);

	}
	public void bindKeyBoard(View v, final Boolean isFromTop){
		emojiView = v;
		emojiView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final EmojiKeyBoardListener listener = new EmojiKeyBoardListener(){
					@Override
					public void onEmojiSelected(View view) {
						// TODO Auto-generated method stub
						String code = ((Emoji) view).getCode();
						Spanned cs = Html.fromHtml("<img src='" + code + "'/>", mEmojiUtil.getImageGetter(), null);
						int start = getSelectionStart();
						EmojiUtil.insertSpannedTextToCurrentPosition(cs, M05RichText.this, start, start);
					}

					@Override
					public void afterClose(View v) {
						// TODO Auto-generated method stub
						showKeyBoard();
					}
				};
				hideKeyBoard();
				EmojiKeyBoard dw = new EmojiKeyBoard(v, isFromTop, listener);
				dw.setOnDismissListener(new OnDismissListener() {

					@Override
					public void onDismiss() {
						// TODO Auto-generated method stub
						showKeyBoard();
					}
				});
				dw.showLikeKeyBoard();
			}
		});
	}
	public void bindKeyBoard(View v){
		emojiView = v;
		emojiView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final EmojiKeyBoardListener listener = new EmojiKeyBoardListener(){
					@Override
					public void onEmojiSelected(View view) {
						// TODO Auto-generated method stub
						String code = ((Emoji) view).getCode();
						Spanned cs = Html.fromHtml("<img src='" + code + "'/>", mEmojiUtil.getImageGetter(), null);
						int start = getSelectionStart();
						EmojiUtil.insertSpannedTextToCurrentPosition(cs, M05RichText.this, start, start);
					}

					@Override
					public void afterClose(View v) {
						// TODO Auto-generated method stub
						showKeyBoard();
					}
				};
				hideKeyBoard();
				EmojiKeyBoard dw = new EmojiKeyBoard(v, listener);
				dw.setOnDismissListener(new OnDismissListener() {

					@Override
					public void onDismiss() {
						// TODO Auto-generated method stub
						showKeyBoard();
					}
				});
				dw.showLikeKeyBoard();
			}
		});
	}

	public EmojiUtil getmEmojiUtil() {
		return mEmojiUtil;
	}

	public void setmEmojiUtil(EmojiUtil mEmojiUtil) {
		this.mEmojiUtil = mEmojiUtil;
	}
}
